

#include <cecore/cedir.h>
#include <windows.h>

ceDir::ceDir ()
	: dir (".")
{
	dir = getCurrentPath ();
}

ceDir::ceDir(const ceString& dir)
	: dir (dir)
{
}


ceString ceDir::getCurrentPath () const
{
	char buffer[1024];
	unsigned length = GetCurrentDirectory (1024, buffer);
	if (length == 0)
	{
		return "";
	}

	return buffer;
}

ceString ceDir::GetCurrentPath () const
{
	return dir;
}

bool ceDir::GoUp ()
{
	return ChangeDir ("..");
}

bool ceDir::ChangeDir (const ceString& directory)
{
	if (!isAbsolutePath (directory))
	{
		if (!SetCurrentDirectory (dir.c_str()))
		{
			return false;
		}
	}
	if (!SetCurrentDirectory(directory.c_str()))
	{
		return false;
	}

	dir = getCurrentPath ();
	return true;
}


bool ceDir::isAbsolutePath (const ceString& path)
{
	if (	path.size () >= 3
		&&	IsCharAlpha (path[0])
		&&	path[1] == ':'
		&&	(path[2] == '\\' || path[2] == '/'))
	{
		return true;
	}
	return false;
}
